/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: ui.hpp
 */

#ifndef _UI_HPP_
#define _UI_HPP_

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

#include "runner.hpp"
#include "wfqueue_manager_proxy.hpp"
#include "generic_exception.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
typedef struct {
	std::string ui_ip_toman; /* IP address to manager */
	std::string ui_port_toman; /* Port to manager */
	std::string ui_ip_tow_bind; /* Bind to workers (Not used for bind) */
	std::string ui_port_tow_bind; /* Bind to workers */
} UIConfig;
}
//-----------------------------------------------------------------------------
// Constants
namespace middleware {
/*!
 * Min port to which the Manager will listen for incoming requests.
 */
const unsigned int kUIMinManPort = 1051;
/*!
 * Limiting port number to 2^16 - 1.
 */
const unsigned int kUIMaxManPort = 65536 - 1;
/*!
 * Number of maximum tasks in a workflow to be randomly generated.
 */
const unsigned int kUIMaxTasksInWorkflow = 10;
/*!
 * Highest number of sec to wait for random generation of workflows: workflow
 * production max period.
 */
const unsigned int kUIMaxWorkflowGeneretionPeriod = 10;
/*!
 * Etiquette for output.
 */
const std::string kUIEtq = "UI: ";
const std::string kUI1Etq = "DataRequestsManager: ";
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*!
 * Implements elements necessary for UI component to run.
 */
class UI : public Runner {
public:
	/*!
	 * Constructus a new, non-configured (invalid port), UI. Address to
	 * manager is set to loopback non valid port and bind address is set
	 * to loopback casual port.
	 */
	UI();
	/*!
	 * Constructs a new, well-configured UI.
	 */
	UI(UIConfig conf);
	/*!
	 * Destroy UI.
	 */
	~UI();
	/*!
	 * Runs UI.
	 */
	void run() {
		this->_ui_thread = boost::shared_ptr<boost::thread>(
				new boost::thread(
						boost::bind(&UI::exec, this)));
	}
	/*!
	 * Join this thread and wait for it to finish.
	 */
	void join() {
		(this->_ui_thread)->join();
	}
private:
	/*!
	 * Creates a random port.
	 */
	std::string createPort();
	/*!
	 * Creates a random variable number of tasks, put the in a workflow and
	 * returns it. This function contains a finite loop and must be called an
	 * infinite number of times. Attention, workflow is created dynamically,
	 * is up to uset to clear it.
	 */
	Workflow* CreateWorkflow();
	/*!
	 * Execution function.
	 */
	void exec();
	/*!
	 * Execution function for requests acceptor.
	 */
	void exec_data_requests();
	/*!
	 * Initialization routine with user.
	 */
	void bonjour() throw(GenericException);
	/*!
	 * Configuration.
	 */
	UIConfig _config;
	/*!
	 * Undrlaying thread. This sends workflows.
	 */
	boost::shared_ptr<boost::thread> _ui_thread;
	/*!
	 * Undrlaying thread. This receives data requests.
	 */
	boost::shared_ptr<boost::thread> _data_requests_thread;
}; /* UI */
} /* middleware */

#endif
